<style>
    /* 搜索区域样式 */
    .layui-col-space10 > [lay-col] {
        padding: 0 10px;
    }
    .layui-input {
        height: 38px;
        line-height: 1.3;
    }
    .companyFormPhotos{
        /* margin-left:140px; */
        line-height: 38px;
        float: left;
    }
    .companyFormPhotos img{
        cursor: pointer;
        height: 38px;
        width: auto;
        max-width: 200px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">产品名称</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productName" class="layui-input" type="text" placeholder="请输入产品名称" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">产品类型</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productTypeName" class="layui-input" type="text" placeholder="请输入产品类型" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">厂家</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productProducerName" class="layui-input" type="text" placeholder="请输入厂家" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">牌号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productMarkName" class="layui-input" type="text" placeholder="请输入牌号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">品种</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productVarietyName" class="layui-input" type="text" placeholder="请输入品种" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">客户规格码</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productSpecCode" class="layui-input" type="text" placeholder="请输入规格码" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">客户物料</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productCode" class="layui-input" type="text" placeholder="请输入物料" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <!--                <div class="lay-btn-rows">-->
                <!--                    <div class="lay-btn-box">-->
                <!--                        <div class="lay-btn-lt">-->
                <!--                            <button id="warehouseProductBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>-->
                <!--                        </div>-->
                <!--                        <div class="lay-btn-rt">-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                </div>-->
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="warehouseProductTable" lay-filter="warehouseProductTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseProductTableBar">
    <!--    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">详情</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stock" style="background-color: #0A8B0A">库存分布</a>
</script>




<!-- 表单弹窗 -->
<script type="text/html" id="warehouseProductForm">
    <form lay-filter="warehouseProductForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">产品类型</label>
            <div class="layui-input-block">
                <select  id="productTypeId" name="productTypeId" lay-filter="productTypeId" lay-verify="required" lay-search required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">品种</label>
            <div class="layui-input-block">
                <select  id="productVarietyId" name="productVarietyId" lay-filter="productVarietyId" lay-verify="required" lay-search required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">材质</label>
            <input type="hidden" id="productMaterialId" name="productMaterialId" />
            <div class="layui-input-block" id="productMaterial">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">产品名称</label>
            <div class="layui-input-block">
                <input id="productName" name="productName" placeholder="请输入产品名称" type="text" class="layui-input" autocomplete="off" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">产品料号编码</label>
            <div class="layui-input-block">
                <input id="productCode" name="productCode" placeholder="请输入产品料号编码" type="text" class="layui-input" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">规格码</label>
            <div class="layui-input-block">
                <input id="productSpecCode" name="productSpecCode" placeholder="请输入规格码" type="text" class="layui-input" autocomplete="off"/>
            </div>
        </div>
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label">图片</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <div id="productImageUrlPath" class="companyFormPhotos"></div>-->
        <!--                <div id="btnProductImageUrl" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传图片</div>-->
        <!--                <input  id="productImageUrl" name="productImageUrl" type="hidden" class="layui-input" lay-verify=""/>-->
        <!--            </div>-->
        <!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">宽度/外径</label>
            <div class="layui-input-block">
                <input id="width" name="width" placeholder="请输入宽度/外径" type="text" class="layui-input" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">长度</label>
            <div class="layui-input-block">
                <input id="length" name="length" placeholder="请输入长度" type="text" class="layui-input" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">厚度/壁厚/直径</label>
            <div class="layui-input-block">
                <input id="diameter" name="diameter" placeholder="请输入厚度/壁厚/直径" type="text" class="layui-input" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">净重（KG）</label>
            <div class="layui-input-block">
                <input id="netWeight" name="netWeight" placeholder="请输入净重" type="text" class="layui-input" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">毛重（KG）</label>
            <div class="layui-input-block">
                <input id="grossWeight" name="grossWeight" placeholder="请输入毛重" type="text" class="layui-input" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属分类</label>
            <input id="productCategoryId" name="productCategoryId" type="hidden"/>
            <div class="layui-input-block" id="productCategory">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">品牌</label>
            <div class="layui-input-block">
                <select  id="productBrandId" name="productBrandId" lay-filter="productBrandId" lay-verify="required" lay-search required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">牌号</label>
            <div class="layui-input-block">
                <select  id="productMarkId" name="productMarkId" lay-filter="productMarkId" lay-verify="required" lay-search required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">生产厂家</label>
            <div class="layui-input-block">
                <select  id="productProducerId" name="productProducerId" lay-filter="productProducerId" lay-verify="required" lay-search required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">主单位</label>
            <div class="layui-input-block">
                <select  id="productMainUnitId" name="productMainUnitId" lay-filter="productMainUnitId" lay-verify="required" lay-search required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">辅单位</label>
            <div class="layui-input-block">
                <select  id="productSubUnitId" name="productSubUnitId" lay-filter="productSubUnitId" lay-verify="required" lay-search required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input id="remark" name="remark" placeholder="请输入备注" type="text" class="layui-input" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseProductFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>


<!-- 库存分布弹窗 -->
<script type="text/html" id="stockDistributionDialog">
    <div class="layui-fluid" style="padding: 15px">

        <!-- 新增产品信息表格 -->

        <table class="layui-table">
            <thead>
            <tr>
                <th>客户产品料号编码</th>
                <th>客户产品名称</th>
                <th>客户规格码</th>
                <th>内部产品类型</th>
                <!--                <th>内部产品料号</th>-->
                <th>厂家</th>
                <th>牌号</th>
                <th>所属客户</th>
                <th>主计量单位</th>
                <th>单价</th>
                <!--                <th>附件</th>-->
                <th>备注</th>
                <!--                <th>操作</th>-->
            </tr>
            </thead>
            <tbody>
            <tr>
                <td id="productCodeCell"></td>
                <td id="productNameCell"></td>
                <td id="productSpecCodeCell"></td>
                <td id="productTypeCell"></td>
                <!--                <td id="thingCodeStatusCell"></td>-->
                <td id="productProducerCell"></td>
                <td id="productMarkCell"></td>
                <td id="customerCell"></td>
                <td id="productMainUnitCell"></td>
                <td id="priceCell"></td>
                <!--                <td id="productTypeCell"></td>-->
                <td id="remarkCell"></td>
                <!--                <td class="td-manage">-->
                <!--                    <button class="layui-btn layui-btn-warm layui-btn-xs" onclick="x_admin_show('客户产品信息详情','kehu_chanpin_xiangqing.html')">详情</button>-->
                <!--                </td>-->
            </tr>

            </tbody>
        </table>


        <!-- 新增搜索区域 -->
        <div class="layui-form" style="margin-bottom: 15px;">
            <div class="layui-row layui-col-space10">

                <div class="layui-col-md3">
                    <select
                            id="warehouseFactoryAdministrationId"
                            name="warehouseFactoryAdministrationId"
                            lay-filter="warehouseFactoryAdministrationId"
                            lay-search></select>
                </div>

                <div class="layui-col-md3">
                    <select
                            id="localFactoryAdministrationId"
                            name="localFactoryAdministrationId"
                            lay-filter="localFactoryAdministrationId"
                            lay-search></select>
                </div>

                <div class="layui-col-md3">
                    <select
                            id="areaFactoryAdministrationId"
                            name="areaFactoryAdministrationId"
                            lay-filter="areaFactoryAdministrationId"
                            lay-search></select>
                </div>

                <div class="layui-col-md3">
                    <input type="text" name="userBundleCode" lay-affix="clear" placeholder="客户捆绑码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-col-md3">
                    <!--                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="searchBtn">-->
                    <!--                        <i class="layui-icon layui-icon-search"></i> 查询-->
                    <!--                    </button>-->
                    <button class="layui-btn" lay-submit lay-filter="searchBtn"><i class="layui-icon"></i></button>
                    <button class="layui-btn" lay-submit lay-filter="clearSearch">重置</button>
                </div>
            </div>
        </div>

        <table class="layui-table" id="warehouseStockTable" lay-filter="stockTable"></table>

    </div>

</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin','upload','tableTreeDj','config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var tableTree = layui.tableTreeDj;

        //列表
        admin.initDataSelect("warehouse/warehouse/admin/administration/list", { limit: 9999 }, "queryWarehouseFactoryAdministrationId", null, "仓库", "name", null);

        // 渲染表格
        var insTb = table.render({
            elem: '#warehouseProductTable',
            url: config.base + 'warehouse/warehouse/productTwo/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', fixed: 'left'}
                ,{field: 'productCode', title: '客户产品料号编码', align: 'center'}
                ,{field: 'productName',title: '客户产品名称', align: 'center'}
                ,{field: 'productSpecCode', title: '客户规格码', align: 'center'}
                ,{field: 'productType', title: '产品类型' , align: 'center'}
                ,{field: 'productCategory', title: '所属分类' , align: 'center'}
                ,{field: 'productProducer', title: '厂家' , align: 'center'}
                ,{field: 'productMark', title: '牌号' , align: 'center'}
                ,{field: 'customer', title: '所属客户' , align: 'center'}
                ,{field: 'productMainUnit', title: '计量单位' , align: 'center'}
                ,{field: 'inventoryNetWeight', title: '库存(净重)' , align: 'center'}
                ,{field: 'inventoryGrossWeight', title: '库存(毛重)' , align: 'center'}
                ,{field: 'inventoryQuantity', title: '库存(数量)' , align: 'center'}

                ,{align: 'center', toolbar: '#warehouseProductTableBar', title: '操作', width: 200, fixed: 'right'}
            ]],
            done: function (res, curr, count) {

            }
        });


        // 显示库存分布弹窗
        function showStockDistribution(data) {
            admin.open({
                type: 1,
                area: ['80%', '700px'],
                title: '库存分布 - ' + data.productName,
                content: $('#stockDistributionDialog').html(),
                success: async function(layero, index) {
                    // 填充产品信息
                    $('#productNameCell').text(data.productName);
                    $('#productCodeCell').text(data.productCode);
                    $('#productSpecCell').text(data.productSpecCode);
                    $('#customerCodeCell').text(data.customerCode);
                    $('#productTypeCell').text(data.productType);
                    $('#thingCodeStatusCell').text(data.thingCodeStatus);
                    $('#productProducerCell').text(data.productProducer);
                    $('#productMarkCell').text(data.productMark);
                    $('#customerCell').text(data.customer);
                    $('#productSpecCodeCell').text(data.productSpecCode);
                    $('#productMainUnitCell').text(data.productMainUnit);
                    $('#priceCell').text('￥' + data.price);
                    $('#remarkCell').text(data.remark ? data.remark : '无');
                    $('#totalStockCell').text(data.netWeight + ' ' + data.productMainUnit);

                    // 初始化表单
                    layui.form.render();

                    // 表格渲染
                    var tableIns = table.render({
                        elem: '#warehouseStockTable',
                        limit:5,
                        url: config.base + 'warehouse/warehouse/productTwo/selectProductDetailTwo?warehouseProductId=' + data.id,
                        toolbar: true,
                        defaultToolbar: ["filter"],
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'creator', title: '序号', width: 80},
                            {field: 'userBundleCode', title: '客户捆包码'},
                            // {field: 'productVariety', title: '厂家'},
                            {field: 'width', title: '宽度mm'},
                            {field: 'productLength', title: '长度mm'},
                            {field: 'diameter', title: '厚度mm'},
                            {field: 'netWeight', title: '净重'},
                            {field: 'grossWeight', title: '毛重'},
                            {field: 'firstDate',  templet: function(d) {
                                    return  d.firstDate ? d.firstDate : '暂无';
                                }, title: '原始入库时间'},
                            {field: 'originalCustomerId', title: '原始货主'},
                            {field: 'transferCode', title: '货权记录编号'},
                            {field: 'warehouseName',templet: function(d) {
                                    return  d.warehouseName ? d.warehouseName : '暂无';
                                }, title: '仓库'},
                            {field: 'warehouseAreaName',  templet: function(d) {
                                    return  d.warehouseAreaName ? d.warehouseAreaName : '暂无';
                                },title: '库区'},
                            {field: 'warehouseShelfSpaceCode',templet: function(d) {
                                    return  d.warehouseShelfSpaceCode ? d.warehouseShelfSpaceCode : '暂无';
                                }, title: '库位'},
                            {field: 'originalTransferOrderTime', templet: function(d) {
                                    return  d.originalTransferOrderTime ? d.originalTransferOrderTime : '暂无';
                                }, title: '货权变更时间'},
                            {field: 'pricingFee', templet: function(d) {
                                    return '￥' + d.pricingFee;
                                }, title: '计价费用'},
                            {field: 'alreadyBilled', templet: function(d) {
                                    return '￥' + d.alreadyBilled;
                                }, title: '已出账'},
                        ]],
                        done: function (res, curr, count) {
                            // 可根据需求在此处做一些操作
                        }
                    });
                    updateLocalFactoryAdministrationParams(null);
                    // 更新库wei下拉框的参数
                    updateLocalInitDataSelectProParams(null);

                    // 动态加载下拉框：入库仓库
                    await admin.initDataSelectPro(
                        "warehouse/warehouse/admin/administration/list",  // 请求地址
                        { limit: 999 }, // 参数
                        "warehouseFactoryAdministrationId",  // 目标下拉框ID
                        "仓库",  // 下拉框标题
                        async (d, obj) => {
                            // 清空表单字段
                            form.val("purchaseSignTaskForm", {
                                inventorySum: "",
                                warehouseFactoryAdministrationName: ""
                            });

                            if (d.value) {
                                // 更新表单字段
                                form.val("purchaseSignTaskForm", {
                                    inventorySum: obj.inventorySum,
                                    warehouseFactoryAdministrationName: obj.name
                                });

                                // 更新库区下拉框的参数
                                updateLocalFactoryAdministrationParams(d.value);
                            }else{
                                // 更新库区下拉框的参数
                                updateLocalFactoryAdministrationParams(null);
                                // 更新库wei下拉框的参数
                                updateLocalInitDataSelectProParams(null);
                            }
                        },
                        "name"
                    );

// 定义一个函数，用于动态更新库区下拉框的请求参数
                    function updateLocalFactoryAdministrationParams(warehouseId) {
                        admin.initDataSelectPro(
                            "warehouse/warehouse/admin/warehouseFactoryArea/list",  // 请求地址
                            { limit: 999, warehouseId: warehouseId }, // 添加动态参数 warehouseId
                            "localFactoryAdministrationId",  // 目标下拉框ID
                            "库区",  // 下拉框标题
                            async (d, obj) => {
                                // 清空表单字段
                                form.val("purchaseSignTaskForm", {
                                    inventorySum: "",
                                    warehouseFactoryAdministrationName: ""
                                });

                                if (d.value) {
                                    // 更新表单字段
                                    form.val("purchaseSignTaskForm", {
                                        inventorySum: obj.inventorySum,
                                        warehouseFactoryAdministrationName: obj.name
                                    });

                                    // 更新库位下拉框的参数
                                    updateLocalInitDataSelectProParams(d.value);
                                }else{
                                    // 更新库区下拉框的参数
                                    updateLocalInitDataSelectProParams(null);
                                }
                            },
                            "name"
                        );
                    }

// 定义一个函数，用于动态更新库位下拉框的请求参数
                    function updateLocalInitDataSelectProParams(warehouseAreaId) {
                        admin.initDataSelectPro(
                            "warehouse/warehouse/admin/warehouseFactoryShelfSpace/list",  // 请求地址
                            { limit: 999, warehouseAreaId: warehouseAreaId }, // 添加动态参数 warehouseAreaId
                            "areaFactoryAdministrationId",  // 目标下拉框ID
                            "库位",  // 下拉框标题
                            async (d, obj) => {
                                // 清空表单字段
                                form.val("purchaseSignTaskForm", {
                                    inventorySum: "",
                                    warehouseFactoryAdministrationName: ""
                                });

                                if (d.value) {
                                    // 更新表单字段
                                    form.val("purchaseSignTaskForm", {
                                        inventorySum: obj.inventorySum,
                                        warehouseFactoryAdministrationName: obj.name
                                    });
                                }
                            },
                            "code"
                        );
                    }
                    // 重置功能
                    layui.form.on('submit(clearSearch)', function(data) {
                        // 清空搜索条件
                        $('#warehouseFactoryAdministrationId').val('');
                        $('#localFactoryAdministrationId').val('');
                        $('#areaFactoryAdministrationId').val('');
                        $('#userBundleCode').val('');
                        // 更新库区下拉框的参数
                        updateLocalFactoryAdministrationParams(null);
                        // 更新库wei下拉框的参数
                        updateLocalInitDataSelectProParams(null);
                        // 重新加载表格数据
                        tableIns.reload({
                            where: {
                                warehouseId: '',
                                warehouseAreaId: '',
                                warehouseShelfSpaceId: '',
                                userBundleCode: ''
                            },
                            page: {
                                curr: 1 // 重置到第一页
                            }
                        });

                        // 渲染表单
                        form.render();
                    });

                    // 搜索功能
                    layui.form.on('submit(searchBtn)', function(data) {
                        // tableIns.reload({where: data.field,page: {curr: 1}},'data');
                        let field = data.field;

                        console.log(field)

                        // 构建查询参数，如果字段为空则不传递该参数
                        let queryParams = {};
                        queryParams.warehouseId = field.warehouseFactoryAdministrationId;
                        queryParams.warehouseAreaId = field.localFactoryAdministrationId;
                        queryParams.warehouseShelfSpaceId = field.areaFactoryAdministrationId;
                        queryParams.userBundleCode = field.userBundleCode


                        // 携带搜索条件请求，页码重置为1
                        tableIns.reload({
                            where: queryParams,
                            page: {
                                curr: 1 // 重置到第一页
                            }
                        });
                        return false; // 阻止表单提交


                    });
                }
            });
        }

        // 工具条点击事件
        table.on('tool(warehouseProductTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { // 详情
                showEditModel(data);
            } else if (obj.event === 'stock') { // 库存分布（新增的库存分布事件）
                showStockDistribution(data);
            }
        });

        //监听排序
        table.on('sort(warehouseProductTable)', function(obj){
            table.reload('warehouseProductTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#warehouseProductBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '640px',
                offset: '65px',
                title: data ? '修改产品' : '添加产品',
                content: $('#warehouseProductForm').html(),
                success: function () {
                    // 查看大图
                    layer.photos({
                        photos: '.companyFormPhotos'
                        ,anim: 5
                    });

                    var parentId ;
                    admin.reqSync('enterprise/dict/admin/dictLabel/list?page=1&limit=10&dictType=sys_device_type_code', {}, function (res) {
                        if (res.code == 200) {
                            parentId = res.data[0].labelValue;
                        }
                    });

                    //自定义验证规则
                    form.verify({
                        isOnly: function (value) {
                            var isOnly = true;
                            var idVal = $("form input[name='id']").val();
                            admin.req('enterprise/purchase/admin/supplierLevel/isOnly', {code:value,id:idVal}, function (res) {
                                if (res.code == 200) {
                                    isOnly = res.data;
                                }
                            }, 'GET',false);
                            if(!isOnly){
                                return "级别编号必须唯一"
                            }
                        }
                    })
                    //产品类型
                    admin.reqSync('enterprise/product/admin/productType/list?page=1&limit=9999', {}, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            loadSelectHtml(res.data, "请选择产品类型", "productTypeId");
                        }
                    }, 'GET');
                    //品种
                    admin.reqSync('enterprise/product/admin/productVariety/list?page=1&limit=9999', {}, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            loadSelectHtml(res.data, "请选择品种", "productVarietyId",1);
                        }
                    }, 'GET');
                    // //材料
                    admin.initDataSelectTree(true, 'enterprise/product/admin/productMaterialCategory/getTreeData', {limit:9999}, 'productMaterial', data?data.productMaterialId:null, "材料", "productMaterialId", function (obj) {});
                    //所属分类
                    admin.initDataSelectTree(true, 'enterprise/product/admin/productCategory/getTreeData', {limit:9999}, 'productCategory', data?data.productCategoryId:null, "所属类型", "productCategoryId", function (obj) {});
                    //品牌
                    admin.reqSync('enterprise/product/admin/productBrand/list?page=1&limit=9999', {}, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            loadSelectHtml(res.data, "请选择品牌", "productBrandId");
                        }
                    }, 'GET');
                    //牌号
                    admin.reqSync('enterprise/product/admin/productMark/list?page=1&limit=9999', {}, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            loadSelectHtml(res.data, "请选择牌号", "productMarkId",2);
                        }
                    }, 'GET');
                    //生产厂家
                    admin.reqSync('enterprise/product/admin/productProducer/list?page=1&limit=9999', {}, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            loadSelectHtml(res.data, "请选择生产商", "productProducerId",3);
                        }
                    }, 'GET');

                    //主/辅单位
                    admin.reqSync('enterprise/product/admin/productUnit/list?page=1&limit=9999', {}, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            loadSelectHtml(res.data, "请选择单位", "productMainUnitId");
                            loadSelectHtml(res.data, "请选择单位", "productSubUnitId");
                        }
                    }, 'GET');

                    form.val('warehouseProductForm', data);
                    $('#btnProductImageUrl').click(uploadImg('btnProductImageUrl','productImageUrlPath','productImageUrl'));



                    // 表单提交事件
                    form.on('submit(warehouseProductFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'warehouse/warehouse/product/update' : 'warehouse/warehouse/product/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('warehouseProductTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }
        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].id}' id='${data[i].id}'>${data[i].varietyName}</option>`;
                } else if (type == 2){
                    html += `<option value='${data[i].id}' id='${data[i].id}'>${data[i].markName}</option>`;
                } else if (type == 3){
                    html += `<option value='${data[i].id}' id='${data[i].id}'>${data[i].producerName}</option>`;
                }else {
                    html += `<option value='${data[i].id}' data='${JSON.stringify(data[i])}'>${data[i].name}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

        // 上传图片
        function uploadImg(_elem, pathId, inputId) {
            upload.render({
                elem: '#' + _elem
                , url: config.upload_url + 'uploadFile'
                , data: {}
                , accept: 'file'
                , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                , headers: { Authorization: 'Bearer ' + config.getToken() }
                , done: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg || "上传成功", { icon: 1 });
                        let imagUrl = res.data.url;
                        $('#' + pathId).html('<img src="' +imagUrl + '"/>');
                        $('#' + inputId).val(imagUrl);
                    } else if (res.code == 502) {
                        return location.replace('member/login.html');
                    } else {
                        layer.msg(res.msg || "上传失败", { icon: 2 });
                    }
                }
            });
        }
    });

</script>